package server;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class Log {
	public static final int WARNING = 1;
	public static final int SEVERE = 2;
	public static final int INFO = 0;
	
	private static final Logger LOG = Logger.getLogger("logger");
	
	
	public Log(){
		try {
			FileHandler fh = new FileHandler("log.txt",true); // true es para habilitar el append
			LOG.addHandler(fh);
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);
			
		} catch (SecurityException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public synchronized void writeLog(int nivel, String src, String metodo,String msj){
		switch(nivel){
			case WARNING: { LOG.logp(Level.WARNING,"- "+src,"- "+metodo,msj+"\n"); break;}
			case INFO: { LOG.logp(Level.INFO,"- "+src,"- "+metodo,msj+"\n"); break;}
			case SEVERE: { LOG.logp(Level.SEVERE,"- "+src,"- "+metodo,msj+"\n"); break;}
		}
		
		
		
	}
	/*
	When a message is logged via a Logger it is logged with a certain log level. The built-in log levels are:
	SEVERE
	WARNING
	INFO
	CONFIG
	FINE
	FINER
	FINEST
	 */
	
	public static void main(String[] args){
		Log log = new Log();
		log.writeLog(Log.INFO, "SERVIDOR", "BD", "OK");
	}
}
